package com.zxl.security.core.social;

import lombok.Data;
import org.springframework.social.security.SocialAuthenticationFilter;
import org.springframework.social.security.SpringSocialConfigurer;

@Data
public class MySpringSocialConfigurer extends SpringSocialConfigurer {
    private String filterProcessesUrl;
    private SocialAuthenticationFilterPostProcessor socialAuthenticationFilterPostProcessor;

    public MySpringSocialConfigurer(String filterProcessesUrl) {
        this.filterProcessesUrl = filterProcessesUrl;
    }

    /**
     * 查看源代码可以看到
     * http.authenticationProvider(
     * new SocialAuthenticationProvider(usersConnectionRepository, socialUsersDetailsService))
     * .addFilterBefore(postProcess(filter), AbstractPreAuthenticatedProcessingFilter.class);
     * 在SpringSocialConfigurer加入过滤器的时候调用了postProcess(filter)，我们可以重写这个方法，对这个过滤器做一些处理
     * @param object 这里就是SocialAuthenticationFilter过滤器对象
     * @return 返回的SocialAuthenticationFilter过滤器对象
     */
    @SuppressWarnings("unchecked")
    @Override
    protected <T> T postProcess(T object) {
        SocialAuthenticationFilter filter = (SocialAuthenticationFilter) super.postProcess(object);
        filter.setFilterProcessesUrl(filterProcessesUrl);// 改变SocialAuthenticationFilter处理的url，默认是以/auth开头
        if (socialAuthenticationFilterPostProcessor != null) {
            socialAuthenticationFilterPostProcessor.process(filter);
        }
        return (T) filter;
    }
}
